package com.admin.utils.minio;

import com.admin.utils.id.IDUtil;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * @author kuaiting
 */
public class MinioUtil {

	/**
	 * @param minioClient
	 * @param fileBucket
	 * @return 访问路径
	 * @throws Exception
	 */
	public static String uploadFileToMinio(String fileBucket, MultipartFile file, MinioClient minioClient) {
		String filename = file.getOriginalFilename();
		String exname = null;

		if (filename != null) {
			exname = filename.substring(filename.lastIndexOf("."));
		}else {
			return null;
		}
		String type = file.getContentType();
		String fileName =null;
		if(type.contains("image")){
			fileName="img/"+IDUtil.createId() + exname;
		}else if(type.contains("video")) {
			fileName="video/"+IDUtil.createId() + exname;
		}else {
			fileName=IDUtil.createId() + exname;
		}
		try {
			PutObjectArgs objectArgs = PutObjectArgs.builder().object(fileName)
					.bucket(fileBucket)
					.contentType(file.getContentType())
					.stream(file.getInputStream(), file.getSize(), -1).build();
			ObjectWriteResponse 	objectWriteResponse = minioClient.putObject(objectArgs);

			return MinioUtil.getShowUrl(objectWriteResponse.object(), fileBucket, minioClient);
		} catch (Exception e) {
			e.printStackTrace();
		}


		return null;
	}

	/**
	 * 通过文件名删除文件
	 *
	 * @param showUrl
	 * @param minioClient
	 * @param fileBucket
	 * @return boolean
	 */
	public static boolean deleteFileByName(String showUrl, MinioClient minioClient, String fileBucket) {
		String[] split = showUrl.split("fileBucket/");
		String[] split1 = split[1].split("X-Amz-Algorithm");
		String fileName = split1[0].replace("?", "");
		try {
			minioClient.removeObject(RemoveObjectArgs.builder().bucket(fileBucket).object(fileName).build());
			return true;
		} catch (Exception e) {
			return false;
		}
	}


	public static void downFileByNameAndBucket(String fileName, String fileBucket, MultipartFile file, MinioClient minioClient, HttpServletResponse response) {
		InputStream in = null;
		try {
			in = minioClient.getObject(new GetObjectArgs(
					DownloadObjectArgs.builder()
							.filename(fileName)
							.bucket(fileBucket)
							.build()));
			response.setContentType(file.getContentType());
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
			IOUtils.copy(in, response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}


	public static String getShowUrl(String fileName, String bucket, MinioClient minioClient) {
		GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
				.method(Method.GET)
				.bucket(bucket)
				.object(fileName)
				.build();
		try {
			return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
		} catch (ErrorResponseException | InsufficientDataException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | InvalidKeyException | InternalException | ServerException e) {
			e.printStackTrace();
		}
		return null;
	}


}
